
(function(){
var query = "";

query = "select * from tbl1 where a is not 1";
console.log(query);
console.log(SQLParser.parse(query));

query = "select * from tbl1 where a is null";
console.log(query);
console.log(SQLParser.parse(query));

query = "select * from tbl1 where a is not null";
console.log(query);
console.log(SQLParser.parse(query));

return;

query = "select prefname, intvalue from preferences where prefname like '%UserSecurity%'";
console.log(query);
console.log(SQLParser.parse(query));
return;

query = "SELECT * FROM a order by field1 collate nocase asc";
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT * FROM a order by field1 collate nocase";
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT * FROM a order by field1";
console.log(query);
console.log(SQLParser.parse(query));
return;

query = "SELECT * FROM tbl1 _tbl1";
console.log(SQLParser.parse(query));

query = "SELECT q FROM tbl1 AS _tbl1";
console.log(SQLParser.parse(query));


query = "SELECT * FROM tbl1 WHERE a IN (SELECT * FROM tbl1)";
console.log(SQLParser.parse(query));

query = "SELECT tbl1.a FROM tbl1 WHERE a IN (SELECT a FROM tbl1)";
console.log(SQLParser.parse(query));

query = "Select sch.ScheduleID,sch.DateStart,sch.DateEnd,sch.TargetStart,sch.TargetEnd,sch.TimeEnd,sch.TimeStart,sch.Status,p.ProjectDescription,s.storename From Schd_CallSchedule sch Inner Join Project p ON sch.ProjectID = p.ProjectID Inner Join Stores s ON sch.StoreID = s._id WHERE sch.ScheduleID = 1";
console.log(SQLParser.parse(query));


query = "INSERT INTO tbl1 VALUES ('a', 'b', 1+2)";
console.log(SQLParser.parse(query));

query = "INSERT INTO tbl1 SELECT tbl1.a FROM tbl1 WHERE a IN (SELECT a FROM tbl1)";
console.log(SQLParser.parse(query));

query = "DELETE * FROM tbl1";
console.log(SQLParser.parse(query));

query = "DELETE * FROM tbl1 WHERE a IN (SELECT a FROM tbl1)";
console.log(SQLParser.parse(query));

query = "DELETE FROM tbl1";
console.log(query);
console.log(SQLParser.parse(query));

query = "delete from Logout where LogoutID > 0 and LogoutID in (select LogoutID from StoreActivity where LoginID > 0 and LogoutID > 0)"
console.log(SQLParser.parse(query));

query = "DELETE FROM tbl1 WHERE a IN (SELECT a FROM tbl1)";
console.log(SQLParser.parse(query));

query = "UPDATE tbl1 SET a = 1, b='b', c = 1 + 1";
console.log(SQLParser.parse(query));

query = "UPDATE tbl1 SET a > 1, b='b', c = 1 + 1 WHERE a = 1 AND a = 2 AND b = 3";
console.log(query);
console.log(SQLParser.parse(query));

query = "Create Table LoginSignature (_id integer not null  primary key autoincrement, LoginID text, StoreID text,LoginTime text, Signature text)";
console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = "Create Table LoginSignature (_id integer not null, LoginID text, StoreID text,LoginTime text, Signature text)";
console.log(SQLParser.parse(query));

query = "Create Table LoginSignature (_id integer not null primary key, LoginID text, StoreID text,LoginTime text, Signature text)";
console.log(SQLParser.parse(query));

query = "Create Table LoginSignature (_id integer primary key, LoginID text, StoreID text,LoginTime text, Signature text)";
console.log(SQLParser.parse(query));

query = "select * from stores where chainid= 1 order by cast(storenumber AS int)";
console.log(SQLParser.parse(query));

query = "Select (Max(VehicleID) + 1) as VehicleID From EmployeeVehicle";
console.log(SQLParser.parse(query));

query = "Select stores.*, test.* from stores where ChainCode = 1 and StoreNumber in (select vhStoreNumber from vhistory where vhChainCode=1) order by cast(StoreNumber as integer) asc"
console.log(SQLParser.parse(query));

query = "Select a, b, * from stores where ChainCode = 1 and StoreNumber in (select vhStoreNumber from vhistory where vhChainCode=1) order by cast(StoreNumber as integer) asc"
console.log(SQLParser.parse(query));

query = "select distinct (Select '>>' from SurveyAnswerSession Where ProductID=sp.ProductID And LocationID= 11 And surveyid = sp.SurveyID And surveyid = 1)As Ans, sp.ProductID, p.ProductName, sas.SessionID, sas.LocationID, sp.Sequence from SurveyProduct sp"
console.log(SQLParser.parse(query));

query = "SELECT a, b, ';' FROM a;asd"
console.log(SQLParser.parse(query));

query = "DROP TABLE IF EXISTS stores;"
console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));


query = "Insert Into LoginSignature (LoginID, StoreID,LoginTime, Signature) values('-1',1,'2012-09-04 12:00:00', 'C:\img.jpg')"
console.log(SQLParser.parse(query));
//console.log(SQLParser.lexer.tokenize(query));

query = "delete from appstatus where Name in ('CHAIN_NUMBER', 'A')"
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = "UPDATE tbl SET a = 1 + 1, b = (SELECT MIN(a) FROM tbl1)"
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'INSERT INTO a (field2) VALUES ("a\'a")';
////console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT fIeLd1, fIELd2 FROM A";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT fIeLd1, fIELd2 FROM A UNION SELECT fIeLd1, fIELd2 FROM A";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT fIeLd1, fIELd2 FROM A RIGHT INNER JOIN B ON A.field1 = B.field1 INNER JOIN C ON C.field1 = B.field1";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT fIeLd1, fIELd2 FROM (SELECT * FROM _a)A INNER JOIN (SELECT * FROM _b)B ON A.field1 = B.field1";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));


query = "SELECT fIeLd1, fIELd2 FROM tbl1 LIMIT 1,10";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "select intValue  from preferences where prefname+'a'='HHPCEnterCheckInOutTime' AND a = 2";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT A.fIeLd1, fIELd2 FROM A RIGHT INNER JOIN B ON A.field1 = B.field1 INNER JOIN C ON C.field1 = B.field1 where prefname='HHPCEnterCheckInOutTime' AND a = 2";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT 1 AS field1, 2 AS field2 FROM a A";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT *, _a._a.field1, a.field1, field1, field1 AS c, _a.* FROM a _a";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT *, *, _a._a.field1, a.field1, field1, field1 AS c, _a.* FROM a _a";
//console.log(SQLParser.lexer.tokenize(query));
console.log(query);
console.log(SQLParser.parse(query));

query = 'SELECT COUNT(*) AS Number FROM stores JOIN Chains ON Chains._id = stores.chainid';
console.log(query);
console.log(SQLParser.parse(query));

query = 'SELECT * FROM a ORDER BY CAST(field1 as int)';
console.log(query);
console.log(SQLParser.parse(query));

query = 'SELECT * FROM a ORDER BY field1';
console.log(query);
console.log(SQLParser.parse(query));

query = 'select * from SurveyAnswerSession where ifnull(PushedAnswerID, "") = ""'
console.log(query);
console.log(SQLParser.parse(query));

query = 'select * from SurveyAnswerSession where cast(PushedAnswerID as int) = ""'
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT tbl1.a, 1 + 1  as 'where' FROM tbl1 WHERE a IN (SELECT a FROM tbl1) group by b order by a";
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = "Create Table LoginSignature (_id integer not null  primary key autoincrement, LoginID text, StoreID text,LoginTime text, Signature text)";
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'SELECT tabl1.* FROM tabl1 WHERE a = 1 AND ( a =2 or a = 3)'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'SELECT tabl1.* FROM tabl1 LEFT JOIN tbl2 ON a = b'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'SELECT tabl1.* FROM tabl1 WHERE a <> b'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'SELECT tabl1.* FROM tabl1 WHERE a IN ("a", "b", "c")'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'SELECT tabl1.* FROM tabl1 WHERE a IN (SELECT a FROM tbl1)'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = "SELECT MAX(field1+1) as 'a', 1+1 as 'b' FROM (SELECT * FROM t1 UNION SELECT * FROM t2)x"
console.log(query);
console.log(SQLParser.parse(query));


query = 'SELECT (SELECT MAX(field1) FROM _tbl) as "a" FROM tabl1 WHERE a IN (SELECT a FROM tbl1 UNION SELECT b FROM tbl2 ORDER BY a)'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = "SELECT MAX(field1+1+1), MAX(field1) as 'a', 1+1 as 'b', 1 as 'c', 's' as 'd' FROM _tbl"
console.log(query);
console.log(SQLParser.parse(query));


query = "SELECT MAX(field1) as 'a', 1+1 as 'b' FROM _tbl INNER JOIN (SELECT * FROM a) ON (SELECT * FROM a)"
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT COUNT(*) AS sum FROM a"
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT a.*, *  FROM a"
console.log(query);
console.log(SQLParser.parse(query));

query="select * from Logout where LogoutID <=0"
console.log(query);
console.log(SQLParser.parse(query));

query="create table SurveyLocation(StoreID int, SurveyID int, LocationID int, Sequence int, AllowRepeat); "
console.log(query);
console.log(SQLParser.parse(query));

query = "Create table if not exists SurveyPicImages(_id integer not null primary key autoincrement, surveycode text)"
console.log(query);
console.log(SQLParser.parse(query));

query = "select ID from messages where [Read] = 1 "
console.log(query);
console.log(SQLParser.parse(query));

query = "select ID from messages where [Read]  IN (SELECT * FROM a)"
console.log(query);
console.log(SQLParser.parse(query));

query = "select ID, [a] from messages where [Read]  IN ('a','b')"
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT * FROM a order by field1 collate nocase asc";
console.log(query);
console.log(SQLParser.parse(query));

query = "SELECT * FROM a order by field1 collate nocase";
console.log(query);
console.log(SQLParser.parse(query));

query = "delete from Login where LoginID = -1";
console.log(query);
console.log(SQLParser.parse(query));

query = "Insert Into StoreActivity (StoreID, LoginID, LogoutID) values(406530,-1,-1001)";
console.log(query);
console.log(SQLParser.parse(query));

//'insert into LocationName(LocationID, LocationName) values(10,"1234567890`~!@#$%^&*()-=_+\][{}|';:""/.,<>?");'

query = "create table vhistory (_id integer not null primary key autoincrement, vhchaincode text, vhstorenumber text, visitid text, confirmationnumber text, idnumber text, employeename text, logindate text, logoutdate text, empauditavg text, emplastaudit text, storeauditavg text, storelastaudit text, loginID )"
console.log(query);
console.log(SQLParser.parse(query));

query = 'insert into shistory(_id, visitid, answerid, surveyversion, surveycode,\t surveytitle, answerdate, auditscore,  locationname, locationseq, productname, completed, modified, CanAudit) values ("18737092","26251599","18737092","2","7255","Android Photo questions","2012-10-12 14:19:53.000","0","","0","","1","2012-10-12 14:21:05.340","0");'
console.log(query);
console.log(SQLParser.parse(query));

query = "create table shistory(_id integer not null primary key autoincrement, visitid text, answerid text, surveyversion text, surveycode text, surveytitle text, answerdate text, auditscore text, locationname text, locationseq text, productname text, completed text, modified text, CanAudit integer )"
console.log(query);
console.log(SQLParser.parse(query));

query = 'select "1","1", s.surveycode from surveys s where s.surveycode in (42,43,100005,100006,100064,100063,100069,100093,100094,100096,100123,100127,100133,100138,100140,7251,7252,7253,7254,355,7255,2228,2229,2230,7258,7259,7260,7262,7263,7264,7265,100147,100148,100152,5,100156,100158,100159,100160,100161,100162,100163,100164,100165,100166,100167,100168,100169,100170,100171,100172,100174,100173,100175,100177,200000,200001,200002,200003,199999,2345,200004,200005,200007,200009,200010,9735,200012,200014,200015,200016,200017,200024,200025,999900,999901,999902,999999,1000012,1000017)'
console.log(query);
console.log(SQLParser.parse(query));

query ='insert into tbl( _id) values ("""U-Scan""");'
console.log(query);
console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

/*
//error due to duplicate _id
'insert into vhistory(_id, vhchaincode, vhstorenumber, visitid, confirmationnumber, idnumber, employeename, logindate, logoutdate, empauditavg, emplastaudit, storeauditavg, storelastaudit, loginID) values ("26251250","14","131","26251250","26270674","1236","Android User #2","2012-10-06 06:42:00.000","2012-10-06 06:43:00.000","0","","0","","26252549");'
*/

query = 'insert into a(f1) values("a", "3M 1"" Wide Masking Tape")'

console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'insert into tstTbl1 values("1""2")'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = 'insert into LocationName(LocationID, LocationName) values(10,"1234567890`~!@#$%^&*()-=_+\\][{}|\';:""/.,<>?");'
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

// query = 'insert into tstTbl1 values("1\\"2")'
// console.log(query);
// console.log(SQLParser.lexer.tokenize(query));
// console.log(SQLParser.parse(query));

query = "insert into tstTbl1 values('1''2')"
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

// query = "insert into tstTbl1 values('1\\'2')"
// console.log(query);
// console.log(SQLParser.lexer.tokenize(query));
// console.log(SQLParser.parse(query));

query = "insert into tstTbl1 values('1\"''')"
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));


query = "INSERT INTO tbl1 ([From], [To]) VALUES(1,1)"
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = "SELECT [From], [To] FROM tbl1"
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

query = "Select aus.StoreID, aus.SurveyID, aus.QuestionNumber, aus.QuestionID, aus.LocationID, aus.ProductID as productIDparent, aus.Answer as AnswerText, aus.Comment as AnswerComment, qu.qnumber, qu._id,qu.qtype,qu.numchoices,qu.isdroplist,qu.parentsurveycode from AutoSave aus, questions qu Where StoreID=1239835 AND SurveyID=100032 and qu._id=aus.QuestionID order by aus.ProductID"
console.log(query);
//console.log(SQLParser.lexer.tokenize(query));
console.log(SQLParser.parse(query));

})();